<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="题目" prop="appraisalName">
        <el-input v-model="queryParams.appraisalName" placeholder="请输入测评名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="appraisalTopicList">
      <el-table-column label="序号" align="center" width="55" type="index" />
       <el-table-column label="分类名称" prop="categoryId" min-width="150" :show-overflow-tooltip="true"  :formatter="formatterCategory"/>
      <el-table-column label="测评名称" prop="appraisalName" min-width="250" :show-overflow-tooltip="true" />
      <el-table-column label="测评题数" align="center" prop="appraisalNumber" min-width="150"
        :show-overflow-tooltip="true" />
      <el-table-column label="答题时间" align="center" prop="answerTime" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="测评真人数" align="center" prop="appraisalPeopleTrue" min-width="150"
        :show-overflow-tooltip="true" />
      <el-table-column label="测评假人数" align="center" prop="appraisalPeopleFalse" min-width="150"
        :show-overflow-tooltip="true" />
      <el-table-column label="报告价格" align="center" prop="reportPrice" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="首页标识" align="center" min-width="100" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.homeStatus" active-value="1" inactive-value="0"
            @change="handleStatusChange(scope.row, 'homeStatus')"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="推荐标识" align="center" min-width="100" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.recommendStatus" active-value="1" inactive-value="0"
            @change="handleStatusChange(scope.row, 'recommendStatus')"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否上下架" align="center" min-width="100" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.lineStatus" active-value="0" inactive-value="1"
            @change="handlelineStatus(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="测评简介" align="center" prop="summary" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="测评介绍" align="center" prop="appraisalIntroduce" min-width="150"
        :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:appraisalTopic:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改评测题目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="测评分类" prop="categoryId">
          <el-select v-model="form.categoryId">
            <el-option v-for="item in AppraisalCategoryData" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="测评名称" prop="appraisalName">
          <el-input v-model="form.appraisalName" placeholder="请输入测评名称" maxlength="20" show-word-limit/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="测评题数" prop="appraisalNumber">
              <el-input-number v-model="form.appraisalNumber" :min="1" :max="1000" :controls="false"
                label="请输入测评题数"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="答题时间" prop="answerTime">
                 <el-input v-model="form.answerTime" placeholder="请输入答题时间" maxlength="20" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="测评真人数" prop="appraisalPeopleTrue">

                <el-input v-model="form.appraisalPeopleTrue" placeholder="请输入测评真人数" maxlength="20" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="测评假人数" prop="appraisalPeopleFalse">
                  <el-input v-model="form.appraisalPeopleFalse" placeholder="请输入测评假人数" maxlength="20" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报告价格" prop="reportPrice">
              <el-input-number v-model="form.reportPrice" :min="0" :max="1000" :controls="false" :precision="2" :step="0.1" label="请输入报告价格"></el-input-number>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="排序" prop="sortOrder">
              <el-input-number v-model="form.sortOrder" :min="1" :max="1000" :controls="false"
                label="请输入排序号，越小越靠前"></el-input-number>
            </el-form-item>

          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="封面图片" prop="coverImage">
              <image-upload v-model="form.coverImage" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="内部图片" prop="interiorImage">
              <image-upload v-model="form.interiorImage" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="首页标识" prop="homeStatus">
              <el-radio-group v-model="form.homeStatus">
                <el-radio v-for="dict in homeStatusList" :key="dict.id" :label="dict.id">{{ dict.name }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否推荐" prop="recommendStatus">
              <el-radio-group v-model="form.recommendStatus">
                <el-radio v-for="dict in homeStatusList" :key="dict.id" :label="dict.id">{{ dict.name
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="是否上下架" prop="lineStatus">
          <el-radio-group v-model="form.lineStatus">
            <el-radio v-for="dict in lineStatusList" :key="dict.id" :label="dict.id">{{ dict.name
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="测评简介" prop="summary">
          <el-input maxlength="100" show-word-limit rows="3" v-model="form.summary" type="textarea"
            placeholder="请输入测评简介" />
        </el-form-item>
         <el-form-item label="指导语" prop="appraisalInstruction">
          <el-input maxlength="100" show-word-limit rows="3" v-model="form.appraisalInstruction" type="textarea"
            placeholder="请输入指导语" />
        </el-form-item>
        <el-form-item label="测评介绍" prop="appraisalIntroduce">
          <el-input type="textarea" maxlength="100" show-word-limit rows="3" v-model="form.appraisalIntroduce"
            placeholder="请输入测评介绍" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input maxlength="100" show-word-limit rows="3" v-model="form.remark" type="textarea"
            placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateAppraisal, listAppraisalTopic, getAppraisalTopic, delAppraisalTopic, addAppraisalTopic, updateAppraisalTopic, appraisalList } from "@/api/evaluation/appraisalTopic";
import { AppraisalCategoryList } from "@/api/select";
export default {
  name: "AppraisalTopic",
  data() {
    return {
      AppraisalCategoryData: [],
      homeStatusList: [
        {
          id: '0',
          name: "否",
        },
        {
          id: '1',
          name: "是",
        }
      ],
      lineStatusList: [
        {
          id: '0',
          name: "上架",
        },
        {
          id: '1',
          name: "下架",
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评测题目表格数据
      appraisalTopicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appraisalId: null,
        divisorId: null,
        topicSerial: null,
        topic: null,
        choiceStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        appraisalInstruction: [
          { required: true, message: "指导语不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "测评分类不能为空", trigger: "blur" }
        ],
        appraisalName: [
          { required: true, message: "测评名称不能为空", trigger: "blur" }
        ],
        appraisalNumber: [
          { required: true, message: "测评题数不能为空", trigger: "blur" }
        ],
        answerTime: [
          { required: true, message: "答题时间不能为空", trigger: "blur" }
        ],
        appraisalPeopleTrue: [
          { required: true, message: "测评真人数不能为空", trigger: "blur" }
        ],
        appraisalPeopleFalse: [
          { required: true, message: "测评假人数不能为空", trigger: "blur" }
        ],
        reportPrice: [
          { required: true, message: "报告价格不能为空", trigger: "blur" }
        ],
        summary: [
          { required: true, message: "测评简介不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getAppraisalCategoryList()
  },
  methods: {
      formatterCategory(row, column) {
      let type = ""
      this.AppraisalCategoryData.forEach(item => {
        if (item.id == row.categoryId) {
          type = item.name
        }
      })
      return type
    },
    getAppraisalCategoryList() {
      AppraisalCategoryList().then(res => {
        this.AppraisalCategoryData = res.rows
      })
    },
    handlelineStatus(row) {
      this.$modal.confirm('确认要修改上下架状态吗？').then(function () {
        return updateAppraisal({
          id: row.id,
          lineStatus: row.lineStatus
        })
      }).then(() => {
        this.$modal.msgSuccess("修改成功")
      }).catch(function () {
        row.lineStatus = row.lineStatus === "1" ? "0" : "1"

      })
    },
    handleStatusChange(row, status) {
      this.$modal.confirm('确认要修改状态吗？').then(function () {
        return updateAppraisal({
          id: row.id,
          homeStatus: row.homeStatus,
          recommendStatus: row.recommendStatus,
        })
      }).then(() => {
        this.$modal.msgSuccess("修改成功")
      }).catch(function () {
        if (status == 'homeStatus') {
          row.homeStatus = row.homeStatus === "1" ? "0" : "1"
        } else {
          row.recommendStatus = row.recommendStatus === "1" ? "0" : "1"
        }
      })
    },
    /** 查询评测题目列表 */
    getList() {
      this.loading = true;
      appraisalList(this.queryParams).then(response => {
        this.appraisalTopicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        appraisalId: null,
        divisorId: null,
        topicSerial: null,
        topic: null,
        choiceStatus: null,
        homeStatus: '1',
        recommendStatus: '1',
        lineStatus: '0',
        remark: null,
        coverImage: null, interiorImage: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加评测题目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids

      getAppraisalTopic(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改评测题目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateAppraisal(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除评测题目编号为"' + ids + '"的数据项？').then(function () {
        return delAppraisalTopic(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/appraisalTopic/export', {
        ...this.queryParams
      }, `appraisalTopic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
